//
//  BPMessagesViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/9.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPMessagesViewController.h"
#import "UIBarButtonItem+Extensions.h"
#import "BPSystemSaleMessagesViewController.h"

@interface BPMessagesViewController ()

@property (weak, nonatomic) IBOutlet UIView *chatView;
@property (weak, nonatomic) IBOutlet UIView *systemMessagesView;
@property (weak, nonatomic) IBOutlet UIView *saleMessagesView;

@property (weak, nonatomic) IBOutlet UILabel *sysMsgLbl;
@property (weak, nonatomic) IBOutlet UILabel *sysMsgTime;
@property (weak, nonatomic) IBOutlet UIImageView *sysMsgRedDot;

@property (weak, nonatomic) IBOutlet UILabel *saleMsgLbl;
@property (weak, nonatomic) IBOutlet UILabel *saleMsgTime;
@property (weak, nonatomic) IBOutlet UIImageView *saleMsgRedDot;

@end

@implementation BPMessagesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息中心";
    [self getMessages];
    
    [self.chatView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushChatWebVC)]];
    [self.systemMessagesView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushSystemMessagesTableViewController)]];
    [self.saleMessagesView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushSaleMessagesTableViewController)]];
}

- (void)pushSystemMessagesTableViewController {
    BPSystemSaleMessagesViewController *systemSaleMessagesVC = [[BPSystemSaleMessagesViewController alloc]init];
    systemSaleMessagesVC.url = @"?c=Msg&&a=sysMsg";
    systemSaleMessagesVC.title = @"系统消息";
    systemSaleMessagesVC.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    [self.navigationController pushViewController:systemSaleMessagesVC animated:YES];
}

- (void)pushSaleMessagesTableViewController {
    BPSystemSaleMessagesViewController *systemSaleMessagesVC = [[BPSystemSaleMessagesViewController alloc]init];
    systemSaleMessagesVC.url = @"?c=Msg&&a=salesMsg";
    systemSaleMessagesVC.title = @"促销消息";
    systemSaleMessagesVC.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    [self.navigationController pushViewController:systemSaleMessagesVC animated:YES];
}

- (void)pushChatWebVC {
    UIViewController *chatVC = [[UIViewController alloc]init];
    chatVC.title = @"在线客服";
    chatVC.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:@"m_phone" highLightImage:@"m_phone" target:self action:@selector(onClickRightItem)];
    [self.navigationController pushViewController:chatVC animated:YES];
    
    UIWebView *webV = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    [chatVC.view addSubview:webV];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kefu.easemob.com/webim/im.html?tenantId=35180&hide=&sat=&resources=&ticket=true"]];
    [webV loadRequest:request];
}

- (void)onClickRightItem{
    NSString *telNumber = [NSString stringWithFormat:@"tel://%@", @"13812345678"];
    NSURL *aURL = [NSURL URLWithString:telNumber];
    if ([[UIApplication sharedApplication] canOpenURL:aURL]) {
        [[UIApplication sharedApplication] openURL:aURL];
    }
}

- (void)getMessages {
    
    [SVProgressHUD showWithStatus:@"读取中..."];
    NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    
    [mgr POST:[NSString stringWithFormat:@"%@?c=Msg&&a=nearMsgList",requestURL]
   parameters:@{@"token" : user[@"token"]}
     progress:^(NSProgress * _Nonnull uploadProgress) {
       
   }
     success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"消息中心%@", responseObject);
         if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
             [SVProgressHUD dismiss];
             long sysMsgNotReadCound = [responseObject[@"data"][@"nearSysMsg"][@"notReadCount"] longValue];
             long saleMsgNotReadCound = [responseObject[@"data"][@"nearSalesMsg"][@"notReadCount"] longValue];
             
             self.sysMsgRedDot.hidden = sysMsgNotReadCound == 0 ? YES : NO;
             self.sysMsgLbl.text = responseObject[@"data"][@"nearSysMsg"][@"notReadTitle"];
             self.sysMsgTime.text = responseObject[@"data"][@"nearSysMsg"][@"notReadTimeStr"];
             
             self.saleMsgRedDot.hidden = saleMsgNotReadCound == 0 ? YES : NO;
             self.saleMsgLbl.text = responseObject[@"data"][@"nearSalesMsg"][@"notReadTitle"];
             self.saleMsgTime.text = responseObject[@"data"][@"nearSalesMsg"][@"notReadTimeStr"];
         }
         else {
             [SVProgressHUD showInfoWithStatus:@"请求失败"];
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [SVProgressHUD dismiss];
             });
         }
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         [SVProgressHUD showInfoWithStatus:@"网络故障"];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [SVProgressHUD dismiss];
         });
     }];
    
}

@end
